package com.gpframework.module.process.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.gpframework.module.module.service.IGpIncomingInspectService;
import com.gpframework.module.process.domain.*;
import com.gpframework.module.process.domain.req.GpProcessProblemTrackUpdate;
import com.gpframework.module.process.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.module.process.mapper.GpProcessProblemTrackMapper;
import com.gpframework.module.process.domain.vo.GpProcessProblemTrackVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName GpProcessProblemTrack
 * @Author gpframework
 * @Date 2023-12-20
 * @Version 1.0.0
 * @Description 加工问题跟踪Service业务层处理
 */
@Service
public class GpProcessProblemTrackServiceImpl extends ServiceImpl<GpProcessProblemTrackMapper, GpProcessProblemTrack> implements IGpProcessProblemTrackService {
    @Autowired
    private IGpProcessFormaldehydeContrastService iGpProcessFormaldehydeContrastService;

    @Autowired
    private IGpProcessDeliveryNoteService iGpProcessDeliveryNoteService;

    @Autowired
    private IGpInspectionTableService iGpInspectionTableService;

    @Autowired
    private IGpRevisionTableService iGpRevisionTableService;

    @Autowired
    private IGpProcessProblemTrackService iGpProcessProblemTrackService;

    @Autowired
    private IGpProcessSchedulingService iGpProcessSchedulingService;

    @Autowired
    private IGpProcessIncomingInspectService iGpProcessIncomingInspectService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2023-12-20
     * @Param [lqw]
     * @Return List<GpProcessProblemTrackVo>
     */
    @Override
    public List<GpProcessProblemTrackVo> listVo(QueryWrapper<GpProcessProblemTrack> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2023-12-20
     * @Param [id]
     * @Return GpProcessProblemTrackVo
     */
    @Override
    public GpProcessProblemTrackVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public boolean copy(Long id, Integer copyNum) {
        GpProcessProblemTrack gpProcessProblemTrack = this.getById(id);
        if (gpProcessProblemTrack == null) {
            return false;
        }
        for (int i = 0; i < copyNum; i++) {
            gpProcessProblemTrack.setId(null);
            this.save(gpProcessProblemTrack);
        }
        return true;
    }

    @Override
    public boolean editBatch(List<GpProcessProblemTrackUpdate> list) {
        ArrayList<GpProcessProblemTrack> updateList = new ArrayList<>();
        for (GpProcessProblemTrackUpdate gpProcessProblemTrackUpdate : list) {
            GpProcessProblemTrack gpProcessProblemTrack = Convert.convert(new TypeReference<GpProcessProblemTrack>() {}, gpProcessProblemTrackUpdate);
            //更新排单、质检、来料
            updateIncoming(gpProcessProblemTrack);
            updateList.add(gpProcessProblemTrack);
        }
        return this.updateBatchById(updateList);
    }

    @Transactional
    public void updateIncoming(GpProcessProblemTrack gpProcessProblemTrack) {
        //修改质检单
        GpProcessIncomingInspect gpProcessIncomingInspect = iGpProcessIncomingInspectService.getById(gpProcessProblemTrack.getIncomingInspectId());
        gpProcessIncomingInspect.setProcessor(gpProcessProblemTrack.getProcessor());
        gpProcessIncomingInspect.setProductName(gpProcessProblemTrack.getProductName());
        gpProcessIncomingInspect.setProductColor(gpProcessProblemTrack.getProductColor());
        gpProcessIncomingInspect.setBatchNo(gpProcessProblemTrack.getBatchNo());
        iGpProcessIncomingInspectService.updateById(gpProcessIncomingInspect);

        //修改排单表
        GpProcessScheduling gpProcessScheduling = iGpProcessSchedulingService.getById(gpProcessProblemTrack.getProcessSchedulingId());
        gpProcessScheduling.setBackerName(gpProcessProblemTrack.getBackerName());
        gpProcessScheduling.setBackerColor(gpProcessProblemTrack.getBackerColor());
        gpProcessScheduling.setProcessor(gpProcessProblemTrack.getProcessor());
        gpProcessScheduling.setProductName(gpProcessProblemTrack.getProductName());
        gpProcessScheduling.setProductColor(gpProcessProblemTrack.getProductColor());
        iGpProcessSchedulingService.updateById(gpProcessScheduling);

        //修改到货单
        GpProcessDeliveryNote gpProcessDeliveryNote = iGpProcessDeliveryNoteService.getById(gpProcessProblemTrack.getProcessDeliveryNoteId());
        gpProcessDeliveryNote.setProcessor(gpProcessProblemTrack.getProcessor());
        gpProcessDeliveryNote.setProductName(gpProcessProblemTrack.getProductName());
        gpProcessDeliveryNote.setProductColor(gpProcessProblemTrack.getProductColor());
        gpProcessDeliveryNote.setProcessor(gpProcessProblemTrack.getProcessor());
        gpProcessDeliveryNote.setProductName(gpProcessProblemTrack.getProductName());
        gpProcessDeliveryNote.setProductColor(gpProcessProblemTrack.getProductColor());
        iGpProcessDeliveryNoteService.updateById(gpProcessDeliveryNote);

    }
}
